package com.wing.quartz.controller.admin;

import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.quartz.model.request.JobInfo;
import com.wing.quartz.service.JobInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 * @author
 */
@Api(value = "Admin-任务信息管理", tags = "#/quartz/jobInfo")
@RestController("jobInfoController")
@RequestMapping("quartz/admin/jobInfo")
public class JobInfoController {

    @Autowired
    private JobInfoService jobInfoService;

    @ApiOperation(value = "查询所有任务信息", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<JobInfo> list(){
        List<JobInfo> list = jobInfoService.list();
        return new PageResult<>(list, list.size());
    }

    @ApiOperation(value = "添加任务信息", notes = "添加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jobInfo", value = "任务信息表", required = true, dataType = "JobInfo", paramType = "form"),
            @ApiImplicitParam(name = "access_token", value = "令牌", required = true, dataType = "String", paramType = "form")
    })
    @PostMapping("/add")
    public JsonResult add(JobInfo jobInfo) {
        jobInfoService.add(jobInfo);
        return JsonResult.success("添加成功");
    }

    @ApiOperation(value = "修改任务信息", notes = "修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jobInfo", value = "任务信息表", required = true, dataType = "JobInfo", paramType = "form"),
            @ApiImplicitParam(name = "access_token", value = "令牌", required = true, dataType = "String", paramType = "form")
    })
    @PutMapping("/update")
    public JsonResult update(JobInfo jobInfo) {
        jobInfoService.update(jobInfo);
        return JsonResult.success("修改成功！");
    }

    @ApiOperation(value = "删除任务信息", notes = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jobName", value = "任务名称", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "jobGroup", value = "任务组名", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", dataType = "String", paramType = "query")
    })
    @DeleteMapping("/delete")
    public JsonResult delete(String jobName, String jobGroup) {
        jobInfoService.delete(jobName, jobGroup);
        return JsonResult.success("删除成功！");
    }

    @ApiOperation(value = "暂停任务信息", notes = "暂停")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jobName", value = "任务名称", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "jobGroup", value = "任务组名", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", dataType = "String", paramType = "query")
    })
    @PutMapping("/pause")
    public JsonResult pause(String jobName, String jobGroup) {
        jobInfoService.pause(jobName, jobGroup);
        return JsonResult.success("暂停成功！");
    }

    @ApiOperation(value = "恢复任务信息表", notes = "恢复")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jobName", value = "任务名称", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "jobGroup", value = "任务组名", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", dataType = "String", paramType = "query")
    })
    @PutMapping("/resume")
    public JsonResult resume(String jobName, String jobGroup) {
        jobInfoService.resume(jobName, jobGroup);
        return JsonResult.success("恢复成功！");
    }

}
